import {Component, Input, OnChanges, OnInit, SimpleChanges} from '@angular/core';
import {GetRTDataByKKSCodesService} from '../../../services/get-rtdata-by-kkscodes.service'

@Component({
  selector: 'app-table-character',
  templateUrl: './table-character.component.html',
  styleUrls: ['./table-character.component.css']
})
export class TableCharacterComponent implements OnInit,OnChanges {

  ngOnChanges(changes: SimpleChanges): void {
    this.totalKks=changes['totalKks']?changes['totalKks'].currentValue:this.totalKks;
    this.fillTable();
  }

  private fillTable() {
    this.getKksRes.getRestKksByComponent(this.componentName).subscribe(
      res=>{
        let kksString=res.map(str=>{
          return this.totalKks+str;
        });
        //历史值
        let time=new Date()
        let endTime=time.getTime();
        let startTimeMonth=endTime-30*24*60*60*1000;
        let startTimeYear=endTime-365*24*60*60*1000;
        let subKks=[kksString[7],kksString[8]];
        this.getKksRes.getKKSData(subKks,startTimeMonth,endTime)
          .subscribe(
            res=>{
              if(res.BasicResponse.succeeded===1){
                res.RTDataSets.forEach(item=>{
                  if(item.kksCode===subKks[0]&&item.kksCode){
                    item.RTDataValues.forEach(it=>{
                      this.windAbandonMonth+=it.Value
                    })
                  }
                  else if(item.kksCode===subKks[1]&&item.kksCode){
                    item.RTDataValues.forEach(it=>{
                      this.avaRateMonth+=it.Value
                    })
                    this.avaRateMonth/=item.RTDataValues.length
                  }
                })
              }
            }
          )
        this.getKksRes.getKKSData(subKks,startTimeYear,endTime)
          .subscribe(
            res=>{
              if(res.BasicResponse.succeeded===1){
                res.RTDataSets.forEach(item=>{
                  if(item.kksCode===subKks[0]&&item.kksCode){
                    item.RTDataValues.forEach(it=>{
                      this.windAbandonYear+=it.Value
                    })
                  }
                  else if(item.kksCode===subKks[1]&&item.kksCode){
                    item.RTDataValues.forEach(it=>{
                      this.avaRateYear+=it.Value
                    })
                    this.avaRateYear/=item.RTDataValues.length;
                  }
                })
              }
            }
          )
        //即时值
        this.getKksRes.getKKsDataSnap(kksString).subscribe(
          res=>{
            let data=[];
            kksString.forEach(l=>{
              res.RTDataSets.forEach(r=>{
                if(l==r.kksCode){
                  if(r.RTDataValues){
                    data.push(r.RTDataValues[0].Value)
                  }
                  else{
                    data.push(NaN);
                  }
                }
                // data.push(100);
              })
            });
            if(data.length===this.character.length){
              this.character=data;
            }
          },
          error=>{
            console.log(error)
          }
        )
      },
      error=>{
        console.error(error)
      }
    )
  }

  @Input() public totalKks:string
  @Input() public capacity:number
  public componentName:string='characterTable'
  public character = []

  public windAbandonMonth:number=0;
  public windAbandonYear:number=0;

  public avaRateMonth:number=0;
  public avaRateYear:number=0;

  constructor(
    private getKksRes:GetRTDataByKKSCodesService
  ) { }

  ngOnInit() {
    for (let i=0;i<12;i++){
      this.character.push(0);
    }
    this.capacity = 1;
    // this.character[0]=this.capacity
  }


}
